home *** CD-ROM | disk | FTP | other *** search
- unit XMLStr;
- interface
- uses
- DB;
-
- function DataSetXMLString(DataSet: TDataSet): String;
-
- implementation
- uses
- SysUtils, TypInfo;
-
- function DataSetXMLString(DataSet: TDataSet): String;
- var
- Str: String;
- i: Integer;
-
- function Print(Str: String): String;
- { Convert a fieldname to a printable name }
- var
- i: Integer;
- begin
- for i:=Length(Str) downto 1 do
- if not (UpCase(Str[i]) in ['A'..'Z','1'..'9']) then
- Str[i] := '_';
- Result := Str
- end {Print};
-
- function EnCode(Str: String): String;
- { Convert memo contents to single line XML }
- var
- i: Integer;
- begin
- for i:=Length(Str) downto 1 do
- begin
- if (Ord(Str[i]) in [1..31]) or (Str[i] = '"') then
- begin
- Insert(''+IntToStr(Ord(Str[i]))+';',Str,i+1);
- Delete(Str,i,1)
- end
- else
- if Str[i] = #0 then Delete(Str,i,1)
- end;
- Result := Str
- end {EnCode};
-
- begin
- ShortDateFormat := 'YYYYMMDD';
- try
- Str := '<?xml version="1.0" standalone="yes"?>';
- Str := Str + '<DATAPACKET Version="2.0">';
- with DataSet do
- begin
- Str := Str + '<METADATA>';
- Str := Str + '<FIELDS>';
- if not Active then
- FieldDefs.Update { get info without opening the database };
- for i:=0 to Pred(FieldDefs.Count) do
- begin
- Str := Str + '<FIELD ';
- if Print(FieldDefs[i].Name) <> FieldDefs[i].Name then { fieldname }
- Str := Str + 'fieldname="' + FieldDefs[i].Name + '" ';
- Str := Str + 'attrname="' + Print(FieldDefs[i].Name) + '" fieldtype="';
- case FieldDefs[i].DataType of
- ftString,
- ftFixedChar,
- ftWideString: Str := Str + 'string';
- ftBoolean: Str := Str + 'boolean';
- ftSmallint: Str := Str + 'i2';
- ftInteger: Str := Str + 'i4';
- ftAutoInc: Str := Str + 'i4" readonly="true" SUBTYPE="Autoinc';
- ftWord, // why not i4 ??
- ftFloat: Str := Str + 'r8';
- ftCurrency: Str := Str + 'r8" SUBTYPE="Money';
- ftBCD: Str := Str + 'fixed';
- ftDate: Str := Str + 'date';
- ftTime: Str := Str + 'time';
- ftDateTime: Str := Str + 'datetime';
- ftBytes: Str := Str + 'bin.hex';
- ftVarBytes,
- ftBlob: Str := Str + 'bin.hex" SUBTYPE="Binary';
- ftMemo: Str := Str + 'bin.hex" SUBTYPE="Text';
- ftGraphic,
- ftTypedBinary: Str := Str + 'bin.hex" SUBTYPE="Graphics';
- ftFmtMemo: Str := Str + 'bin.hex" SUBTYPE="Formatted';
- ftParadoxOle,
- ftDBaseOle: Str := Str + 'bin.hex" SUBTYPE="Ole'
- end;
- if FieldDefs[i].Required then Str := Str + '" required="true';
- if FieldDefs[i].Size > 0 then Str := Str + '" WIDTH="' + IntToStr(FieldDefs[i].Size);
- Str := Str + '"/>'
- end;
- Str := Str + '</FIELDS>';
- Str := Str + '</METADATA>';
- if not Active then Open;
- Str := Str + '<ROWDATA>';
- while not Eof do
- begin
- Str := Str + '<ROW ';
- for i:=0 to Pred(Fields.Count) do
- if (Fields[i].AsString <> '') and
- ((Fields[i].DisplayText = Fields[i].AsString) or
- (Fields[i].DisplayText = '(MEMO)')) then
- Str := Str + Print(Fields[i].FieldName) + '="' +
- EnCode(Fields[i].AsString) + '" ';
- Str := Str + '/>';
- Next
- end;
- Str := Str + '</ROWDATA>'
- end;
- Str := Str + '</DATAPACKET>'
- finally
- Result := Str
- end
- end;
-
- end.
-